package com.baomidou.mybatisplus.core.override

import org.apache.ibatis.session.SqlSession
import java.lang.reflect.Method
import java.lang.reflect.Proxy
import java.util.concurrent.ConcurrentHashMap

/**
 *
 * 替换掉引用<br></br>
 * 重写类： org.apache.ibatis.binding.MapperProxyFactory
 *
 */
internal class XmlLessPageMapperProxyFactory<T>(
    private val mapperInterface: Class<T>
) {

  private val methodCache: MutableMap<Method, MybatisMapperProxy.MapperMethodInvoker> = ConcurrentHashMap()

  fun newInstance(sqlSession: SqlSession): T {
    val mapperProxy = XmlLessPageMapperProxy(sqlSession, mapperInterface, methodCache)
    return newInstance(mapperProxy)
  }

  private fun newInstance(mapperProxy: XmlLessPageMapperProxy<T>?): T {
    @Suppress("UNCHECKED_CAST")
    return Proxy.newProxyInstance(
        mapperInterface.classLoader,
        arrayOf<Class<*>>(mapperInterface), mapperProxy
    ) as T
  }

}
